/**
 * Name: DuckHuntPanel
 * @author Zhongtian Jiang
 * @version 1.0
 * This is a class to create DuckHuntPanel
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class DuckHuntPanel extends JPanel implements MouseListener, MouseMotionListener{

	/**
	 * Field SANBAILIU.
	 * (value is 360)
	 */
	private static final int SANBAILIU = 360;

	/**
	 * Field YIBAI.
	 * (value is 100)
	 */
	private static final int YIBAI = 100;

	/**
	 * Field BASHI.
	 * (value is 80)
	 */
	private static final int BASHI = 80;

	/**
	 * Field TIBAIQI.
	 * (value is 170)
	 */
	private static final int TIBAIQI = 170;

	/**
	 * Field YIBAIJIU.
	 * (value is 190)
	 */
	private static final int YIBAIJIU = 190;

	/**
	 * Field SI.
	 * (value is 4)
	 */
	private static final int SI = 4;

	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;

	/**
	 * Field JIU.
	 * (value is 9)
	 */
	private static final int JIU = 9;

	/**
	 * Field WU.
	 * (value is 0.05)
	 */
	private static final float DIANLINGWU = 0.05f;

	/**
	 * Field ERBAIWU.
	 * (value is 250)
	 */
	private static final int ERBAIWU = 250;

	/**
	 * Field THREEFIVE.
	 * (value is 350)
	 */
	private static final int SANBAIWU = 350;

	/**
	 * Field FOUR.
	 * (value is 400)
	 */
	private static final int SIBAI = 400;

	/**
	 * Field TWO.
	 * (value is 200)
	 */
	private static final int ERBAI = 200;

	/**
	 * Field FIVE.
	 * (value is 5)
	 */
	private static final int WU = 5;

	/**
	 * Field a.
	 */
	public List<Duck> aaa;
	
	/**
	 * Field updater.
	 */
	public DuckUpdater updater;
	
	/**
	 * Field projectiles.
	 */
	/**
	 * Field cury.
	 */
	/**
	 * Field curx.
	 */
	/**
	 * Field n.
	 */
	public int nnn=WU, curx, cury, projectiles;
	
	/**
	 * Field lose.
	 */
	public boolean lose;
	
	/**
     * constructor

     * @param n int
	 * @param projectiles int
	 */
	public DuckHuntPanel(int n, int projectiles){
		lose=false;
		curx=ERBAI;
		cury=ERBAI;
		nnn=n;
		this.projectiles=projectiles;
		setPreferredSize(new Dimension(SIBAI, SIBAI));
		setBackground(Color.blue);
		aaa=new ArrayList<Duck>();
		aaa.clear();
		for (int i=0;i < nnn;i++){
			aaa.add(new Duck(new Random().nextInt(SANBAIWU),
					new Random().nextInt(ERBAIWU), new ImageIcon("duck.png").getImage()));
		}
		addMouseListener(this);
		addMouseMotionListener(this);
		updater=new DuckUpdater(this, DIANLINGWU);
		updater.start();
	}
	
	/**
     * update the location of ducks

     */
	public void updateTargets(){
		for (int i=0;i < aaa.size();i++){
			int tx=aaa.get(i).getX() + new Random().nextInt(WU);
			int ty=aaa.get(i).getY() + new Random().nextInt(JIU) - SI;
			tx=tx % SANBAIWU;
			if (ty < 0) ty=0;
			if (ty > ERBAIWU) ty=ERBAIWU;
			aaa.get(i).setX(tx);
			aaa.get(i).setY(ty);
		}
	}
	
	/**
     * paint the graph

     * @param g Graphics
	 */
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.green);
		g.drawImage(new ImageIcon("grass.jpg").getImage(), 0, 0, SIBAI, SIBAI, null);
		//g.fillRect(0, 300, 400, YIBAI);
		for (int i=0;i < aaa.size();i++){
			aaa.get(i).draw(g);
		}
		g.drawOval(curx - WU, cury - WU, SHI, SHI);
		g.drawLine(curx, cury - WU, curx, cury + WU);
		g.drawLine(curx - WU, cury, curx + WU, cury);
		g.setColor(Color.black);
		
		if (lose){
			g.drawString("YOU LOSE!", TIBAIQI, YIBAIJIU);
			g.drawImage(new ImageIcon("lose.gif").getImage(), 
					YIBAI, ERBAI, BASHI, BASHI, null);
			PlayerInfo.DuckKilled=SHI - aaa.size();
			DuckHuntMain.Frame.dispose();
			PlayerInfo.Money+=PlayerInfo.DuckKilled * YIBAI;
			
			if (!DuckHuntMain.Flag) {
				JOptionPane.showMessageDialog(
					ConfigurationScreen.MyFrame,
					"You exchanged the duck meat for " + PlayerInfo.DuckKilled * YIBAI);
				DuckHuntMain.Flag=true;
				MarketPlaceScreen.Money.setText("" + PlayerInfo.getMoney());
			}
		}else if (aaa.size() == 0){
			PlayerInfo.DuckKilled=SHI;
			g.drawString("YOU WIN!", TIBAIQI, YIBAIJIU);
			DuckHuntMain.Frame.dispose();
			PlayerInfo.Money+=PlayerInfo.DuckKilled * YIBAI;
			if (!DuckHuntMain.Flag) {
				JOptionPane.showMessageDialog(
					ConfigurationScreen.MyFrame,
					"You exchanged the duck meat for " + PlayerInfo.DuckKilled * YIBAI);
				DuckHuntMain.Flag=true;
				MarketPlaceScreen.Money.setText("" + PlayerInfo.getMoney());
			}
		}
		else g.drawString("Ducks: " + aaa.size() +
				" Projectiles: " + projectiles, 0, SANBAILIU);
	}
	
	/**
     * perform the action when mouse is moved.

     * @param e MouseEvent
	
	 * @see java.awt.event.MouseMotionListener#mouseMoved(MouseEvent) */
	@Override
	public void mouseMoved(MouseEvent e) {
		curx=e.getX();
		cury=e.getY();
		repaint();
	}
	
	/**
     * perform the action when mouse is clicked, like some ducks is hit and etc.

     * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseClicked(MouseEvent) */
	@Override
	public void mouseClicked(MouseEvent e) {
		curx=e.getX();
		cury=e.getY();
		projectiles--;
		for (int i=0;i < aaa.size();i++){
			if (aaa.get(i).canHit(curx, cury)){
				aaa.remove(i);
			}
		}
		if (aaa.size() > 0 && projectiles == 0){
			lose=true;
		}
		repaint();
	}

	/**
	 * Method mouseDragged.
	 * @param arg0 MouseEvent
	
	 * @see java.awt.event.MouseMotionListener#mouseDragged(MouseEvent) */
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}

	/**
	 * Method mouseEntered.
	 * @param arg0 MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseEntered(MouseEvent) */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}

	/**
	 * Method mouseExited.
	 * @param arg0 MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseExited(MouseEvent) */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}

	/**
	 * Method mousePressed.
	 * @param arg0 MouseEvent
	
	 * @see java.awt.event.MouseListener#mousePressed(MouseEvent) */
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}

	/**
	 * Method mouseReleased.
	 * @param arg0 MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseReleased(MouseEvent) */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}
}
